# _*_ coding: UTF-8 _*_
# @Time     : 2021/11/3 下午 02:18
# @Author   : Li Jie
# @Site     : http://www.hzdledu.cn/
# @File     : login_page.py
# @Software : PyCharm
from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait  # 等待类
from selenium.webdriver.support import expected_conditions  # 等待条件
from selenium.webdriver.common.by import By  # 定位符


class LoginPage():
    # 定义类的属性并完成初始化
    def __init__(self, driver):
        self.user_locator = (By.XPATH, '//*[@id="account"]/input')  # 账号输入框定位符
        self.pwd_locator = (By.XPATH, '//*[@id="password"]/input')  # 密码输入框定位符
        self.vcode_locator = (By.XPATH, '//*[@id="authCode"]/div/input')  # 验证码输入框定位符
        self.login_locator = (By.XPATH, '//*[@id="root"]/div/div[1]/form/div[5]/div/div/span/button')  # 登录按键定位符
        self.system_locator = (By.XPATH, '//*[@id="root"]/section/aside/div/ul/li[4]/div[1]/span')
        self.city_locator = (By.XPATH, '//*[@id="sys$Menu"]/li[1]/a')
        self.add_locator = (By.XPATH, '//*[@id="root"]/section/section/main/div/div[1]/button[1]')
        self.driver = driver
        # self.driver = webdriver.Chrome()  # 调试使用

        self.wait = WebDriverWait(driver, 30)

    # 页面操作方法
    # 输入账号
    def input_user(self, user):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(self.user_locator))
            self.driver.find_element(*self.user_locator).clear()
            self.driver.find_element(*self.user_locator).send_keys(user)
        except Exception as e:
            self.driver.get_screenshot_as_file('./log/输入账号错误.png')
            raise e

    # 输入密码
    def input_pwd(self, pwd):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(self.pwd_locator))
            self.driver.find_element(*self.pwd_locator).clear()
            self.driver.find_element(*self.pwd_locator).send_keys(pwd)
        except Exception as e:
            self.driver.get_screenshot_as_file('./log/输入密码错误.png')
            raise e

    # 输入验证码
    def input_vcode(self, vcode):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(self.vcode_locator))
            self.driver.find_element(*self.vcode_locator).clear()
            self.driver.find_element(*self.vcode_locator).send_keys(vcode)
        except Exception as e:
            self.driver.get_screenshot_as_file('./log/输入验证错误.png')
            raise e

    # 点击登录按键
    def click_login(self):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(self.login_locator))
            self.driver.find_element(*self.login_locator).click()
        except Exception as e:
            self.driver.get_screenshot_as_file('./log/点击登录错误.png')
            raise e

    # 点击系统管理
    def click_system(self):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(self.system_locator))
            self.driver.find_element(*self.system_locator).click()
        except Exception as e:
            self.driver.get_screenshot_as_file('./log/点击系统管理错误.png')
            raise e

    # 点击城市列表
    def click_city(self):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(self.city_locator))
            self.driver.find_element(*self.city_locator).click()
        except Exception as e:
            self.driver.get_screenshot_as_file('./log/城市列表错误.png')
            raise e

    # 点击添加按键
    def click_add(self):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(self.add_locator))
            self.driver.find_element(*self.add_locator).click()
        except Exception as e:
            self.driver.get_screenshot_as_file('./log/点击添加错误.png')
            raise e

    # 输入城市名
    def input_su(self, su):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(self.su_locator))
            self.driver.find_element(*self.user_locator).clear()
            self.driver.find_element(*self.user_locator).send_keys(su)
        except Exception as e:
            self.driver.get_screenshot_as_file('./log/输入账号错误.png')
            raise e


# 调试
if __name__ == "__main__":
    driver = webdriver.Chrome()
    driver.get('http://192.168.6.189/cloud/#/open/login')
    login_page = LoginPage(driver)

    login_page.input_user('admin')
    time.sleep(1)
    login_page.input_pwd('123456')
    time.sleep(1)
    login_page.input_vcode('8888')
    time.sleep(1)
    login_page.click_login()
    time.sleep(1)
    login_page.click_system()
    time.sleep(1)
    login_page.click_city()
    time.sleep(1)
    login_page.click_add()
    time.sleep(1)

    driver.quit()
